<?php

class Str {

    public $str;

    public function __construct($str) {

        $this->str = $str;
    }

    static public function trim($str) {

        return preg_replace('/\s+/', '', $str);
    }

    function strToBin(){
        $arr = preg_split('/(?<!^)(?!$)/u', $this->str);
        foreach($arr as &$v){
            $temp = unpack('H*', $v);
            $v = base_convert($temp[1], 16, 2);
            unset($temp);
        }

        return join(' ',$arr);
    }

    function binToStr(){
        $arr = explode(' ', $this->str);
        foreach($arr as &$v){
            $v = pack("H".strlen(base_convert($v, 2, 16)), base_convert($v, 2, 16));
        }

        return join('', $arr);
    }

    static function getLine($str, $num = 0) {

        $arr = explode("\n", $str);
        if(!$arr) {
            return null;
        }
        return $arr[$num];
    }

    static function randomFloat($min = 0, $max = 1) {
        return $min + mt_rand() / mt_getrandmax() * ($max - $min);
    }
    /**
     * @param $search
     * @param $array
     * @return bool
     * @throws Exception
     */
    public static function in_array($search, $array) {

        if(!is_array($array)) {

            throw new Exception('2th param should be array');
        }

        if(is_object($search)) {

        } else if(is_array($search)) {

            foreach ($array as $key => $value) {

            }
        } else {

            $search = strval($search);
            if(in_array($search, $array)) {
                return true;
            }
        }

        return false;
    }

    /**
     * @param $str
     * @param $arr
     * @return bool
     */
    public static function strContainArray($str, $arr) {

        foreach ($arr as $value) {

            if(strpos($str, $value) !== false) {
                return true;
            }
        }
        return false;
    }

    /**
     * @param int $length
     * @return int
     */
    public static function getRandom($length = 6) {
        $min = pow(10 , ($length - 1));
        $max = pow(10, $length) - 1;
        return mt_rand($min, $max);
    }
}